#!/usr/bin/perl -w 

use English;

@benchmarks = ("JPEG");

@file_list = ("CUDA/JPEG/output_JPEG.txt");

$result_file = "warps.txt";

open(OUTPUT, ">$result_file") || die "Cannot open file $result_file for writing\n";

for ($j = 0; $j <= $#file_list; $j++) {  

	
	my @warps = `grep "num_warps_issuable" $file_list[$j] | tail -n 1 |  awk -F ':' '\{print \$2\}'`;
	chomp(@warps);
        push @globalwarps, \@warps;
	$sum = sum(@globalwarps);
 } 

    # BASELINE Starts ----------------------------------------------------------------
 	# General ----------------------------------
	print OUTPUT "Benchmarks \t";
	
	
	print OUTPUT "\n";
	print OUTPUT "Sum\t";

for ($k = 0;$k <= $#file_list;$k++) 
{
    # BASELINE Starts ----------------------------------------------------------------
	print OUTPUT "$benchmarks[$k] \t";
	print OUTPUT "$globalwarps[$k][0] \t";
		
	print OUTPUT "\n";
}


sub avg {
	@_ == 1 or die ('Sub usage: $avg = avg(\@array);');
	my ($array_ref) = @_;
	my $sum;
	my $count = scalar @$array_ref;
        if ($count == 0) {
                return 0;
        }
	foreach (@$array_ref) { $sum += $_; }
	return $sum / $count;
}

sub max {
	@_ == 1 or die ('Sub usage: $max = max(\@array);');
	my ($array_ref) = @_;
	my $maxval = -999999999999999999;
	foreach (@$array_ref)
	{
		if ( $maxval < $_ )
		{
			$maxval = $_;
		}
	}
	return $maxval;
}

sub sum {
	@_ == 1 or die ('Sub usage: $sum = sum(\@array);');
	my ($array_ref) = @_;
	my $sum;
	foreach (@$array_ref) { $sum += $_; }
	return $sum;
}


	# my $L1accuracy_last = `grep gpgpu_prefetch_L1_accuracy $file_list[$j] | tail -n 1 | awk '\{print \$3\}'`;
	# chomp($L1accuracy_last);
	# push @globalL1accuracy_last, $L1accuracy_last;
	
	# my $L2accuracy_last = `grep gpgpu_prefetch_L2_accuracy $file_list[$j] | tail -n 1 | awk '\{print \$3\}'`;
	# chomp($L2accuracy_last);
	# push @globalL2accuracy_last, $L2accuracy_last;



